/**
 * @description: 存储库: 标签页
 */
const useTabsViewStore = defineStore("useTabsViewStore", {
  state() {
    return {
      tabsViewList: [], //标签页数据存储
    };
  },
  actions: {
    // 添加
    add(tab) {
      if (!this.tabsViewList.find(({ fullPath }) => fullPath === tab.fullPath)) {
        this.tabsViewList.push(tab);
      }
    },
    // 关闭: 当前
    close(fullPath, isCurrent, callback) {
      // 如果关闭的是当前标签, 则自动跳转到最近一项
      if (isCurrent) {
        this.tabsViewList.forEach((item, index) => {
          if (item.fullPath === fullPath) {
            const recentItem = this.tabsViewList[index + 1] || this.tabsViewList[index - 1];
            if (recentItem) callback(recentItem.fullPath);
          }
        });
      }
      this.tabsViewList = this.tabsViewList.filter((item) => item.fullPath !== fullPath);
    },
    // 关闭: 其他
    closeOther(fullPath, isCurrent, callback) {
      // 如果关闭的不是当前标签, 则跳转到剩余的那一项
      this.tabsViewList = this.tabsViewList.filter((item) => {
        return item.fullPath === fullPath;
      });
      if (!isCurrent) callback(this.tabsViewList[0].fullPath);
    },
  },
  // 永久持久化
  persist: {
    enabled: true,
    strategies: [
      {
        storage: localStorage,
        paths: ["tabsViewList"],
      },
    ],
  },
});

export default useTabsViewStore;
